var reportRequest, bdiDiv, download;

function setReportRequest() {
	//Setting the bdi
	bdiDiv = document.createElement("div");
	document.body.appendChild(bdiDiv);
	application.addLibrary("/cordys/wcp/library/data/busdataisland.htm", bdiDiv);
	bdiDiv.async= true;
	bdiDiv.onresponse= "getPdfUrl()";
	bdiDiv.id='bdiId';
	bdiDiv.cordysType="wcp.library.data.BusDataIsland";

	//loading the request
        requestStr='<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/"><SOAP:Body><RunReport xmlns="http://schemas.cordys.com/bsf/uc/webapps/1.0/report"><REPORTTYPE>adhoc</REPORTTYPE><ISMODIFIED>n</ISMODIFIED><REPORT><REPORTXML><REPORTID>376eec84-0ae9-1584-006d-d8f1d05ccf7e</REPORTID><reportformat>pdf</reportformat><reportinput xmlns="http://schemas.cordys.com/bsf/uc/webapps/1.0/report" reportType="IMPORTED" label="Invoicecordys" reportformat="pdf" id="Invoicecordys" name="Invoicecordys"><param0 formlabel="BusinessPartnerID" inputid="" id="param0" xpath="reportinput/param0" class="java.lang.String" parameterName="GetBusinesspartners_BusinessPartnerID"></param0><param1 formlabel="BusinessPartnerID" inputid="" id="param1" xpath="reportinput/param1" class="java.lang.String" parameterName="GetBusinessPartnerLocations_BusinessPartnerID"></param1><param2 formlabel="LocationID" inputid="" id="param2" xpath="reportinput/param2" class="java.lang.String" parameterName="GetBusinessPartnerLocations_LocationID"/><param3 formlabel="BusinessPartnerID" inputid="" id="param3" xpath="reportinput/param3" class="java.lang.String" parameterName="GetBusinessPartnerRoles_BusinessPartnerID"></param3><param4 formlabel="BusinessRole" inputid="" id="param4" xpath="reportinput/param4" class="java.lang.String" parameterName="GetBusinessPartnerRoles_BusinessRole"></param4><param5 formlabel="InvoiceID" inputid="" id="param5" xpath="reportinput/param5" class="java.lang.String" parameterName="GetInvoiceHeaders_InvoiceID"></param5><param6 formlabel="ContactID" inputid="" id="param6" xpath="reportinput/param6" class="java.lang.String" parameterName="GetContacts_ContactID"></param6><param7 formlabel="PaymentTerm" inputid="" id="param7" xpath="reportinput/param7" class="java.lang.String" parameterName="GetTermsofpayment_PaymentTerm"/><param8 formlabel="SiteName" inputid="" id="param8" xpath="reportinput/param8" class="java.lang.String" parameterName="GetDetailsOfSiteForReport_SiteName"></param8><param9 formlabel="InvoiceID" inputid="" id="param9" xpath="reportinput/param9" class="java.lang.String" parameterName="GetInvoiceLinesByInvoiceIDForiReport_InvoiceID"></param9></reportinput></REPORTXML></REPORT></RunReport></SOAP:Body></SOAP:Envelope>';
	reportRequest = cordys.loadXMLDocument(requestStr);
}

function fireRunReport() {
     setReportRequest();
     fillInputParameters();
     bdiDiv.clear();
     bdiDiv.request = reportRequest;
     bdiDiv.sendAndWait(reportRequest);	
}

function fillInputParameters() {
        var data = InvoiceHeadersModel.getActiveBusinessObject().businessObject;
        if(!data)
            return;
        else
	var invoiceId = cordys.getTextContent(cordys.selectXMLNode(data, ".//*[local-name() = 'InvoiceID']"));
	var businessPartnerId = cordys.getTextContent(cordys.selectXMLNode(data, ".//*[local-name() = 'BusinessPartnerID']"));
	var paymentTerm = cordys.getTextContent(cordys.selectXMLNode(data, ".//*[local-name() = 'TermsofPayment']"));
        var SiteId = cordys.getTextContent(cordys.selectXMLNode(data, ".//*[local-name() = 'Extra9']"));
        var ContactID= cordys.getTextContent(cordys.selectXMLNode(data, ".//*[local-name() = 'CustomerContact']"));
	cordys.setTextContent(cordys.selectXMLNode(reportRequest, ".//*[local-name() = 'param0']"), businessPartnerId);
	cordys.setTextContent(cordys.selectXMLNode(reportRequest, ".//*[local-name() = 'param1']"), businessPartnerId);
	//cordys.setTextContent(cordys.selectXMLNode(reportRequest, ".//*[local-name() = 'param2']"), paymentTerm);
	cordys.setTextContent(cordys.selectXMLNode(reportRequest, ".//*[local-name() = 'param3']"), businessPartnerId);
	cordys.setTextContent(cordys.selectXMLNode(reportRequest, ".//*[local-name() = 'param4']"), "Customer");
	cordys.setTextContent(cordys.selectXMLNode(reportRequest, ".//*[local-name() = 'param5']"), invoiceId);
	cordys.setTextContent(cordys.selectXMLNode(reportRequest, ".//*[local-name() = 'param6']"), ContactID);
	cordys.setTextContent(cordys.selectXMLNode(reportRequest, ".//*[local-name() = 'param7']"), paymentTerm);
	cordys.setTextContent(cordys.selectXMLNode(reportRequest, ".//*[local-name() = 'param8']"), SiteId);
        cordys.setTextContent(cordys.selectXMLNode(reportRequest, ".//*[local-name() = 'param9']"), invoiceId);    
        //cordys.setTextContent(cordys.selectXMLNode(reportRequest, ".//*[local-name() = 'param10']"), ContactID);         
}

function getPdfUrl(eventObject) {
	var path = cordys.getTextContent(cordys.selectXMLNode(application.event.response, ".//*[local-name() = 'REPORTPATH']"));
        fld_ReportURL.setValue(path);
        attachDownloader();
        var downloader = document.getElementById("downloader");
        downloader.setFrameId("RS_IREPORT2Frame");
        downloader.openInNewWindow = false;
        downloadFile(path); 
}